1 module hip.event.handlers.button;
2 
3 public import hip.api.input.button;
4 import hip.util.time;
5 
6 
7 final class HipButtonMetadata : AHipButtonMetadata
8 {
9     import hip.config.opts;
10 
11     float lastDownTime, downTimeStamp;
12     float lastUpTime, upTimeStamp;
13 
14     bool _isPressed = false;
15     bool _isNewState = false;
16     float timeStartedCheckingRestart = 0;
17     float timeUntilRestartMulticlick = HIP_DEFAULT_TIME_UNTIL_CLICK_COUNT_RESTART;
18 
19     ubyte clickCount = 0;
20 
21     override bool isNewState() const {return _isNewState;}
22     override bool isPressed() const {return _isPressed;}
23     override float getLastDownTimeDuration() const {return lastDownTime;}
24     override float getLastUpTimeDuration() const {return lastUpTime;}
25 
26     this(int key){super(key);}
27 
28     override float getDownTimeDuration() const
29     {
30         if(_isPressed)
31             return (HipTime.getCurrentTimeAsMilliseconds() - downTimeStamp) / 1000;
32         return 0;
33     }
34     override float getUpTimeDuration() const
35     {
36         if(!_isPressed)
37             return (HipTime.getCurrentTimeAsMilliseconds() - upTimeStamp) / 1000;
38         return 0;
39     }
40     override void setPressed(bool press)
41     {
42         _isNewState = false;
43         float currTime = HipTime.getCurrentTimeAsMilliseconds();
44         if(currTime - timeStartedCheckingRestart > timeUntilRestartMulticlick)
45         {
46             clickCount = 0;
47             timeStartedCheckingRestart = currTime;
48         }
49         if(press != _isPressed)
50         {
51             _isNewState = true;
52             if(_isPressed)
53             {
54                 lastDownTime = getDownTimeDuration();
55                 upTimeStamp = currTime;
56                 clickCount++;
57             }
58             else
59             {
60                 lastUpTime = getUpTimeDuration();
61                 downTimeStamp = currTime;
62             }
63             _isPressed = press;
64         }
65     }
66 }